//
//  UIImage+ZUtilities.m
//  ZPlayer
//
//  Created by Zed Link on 26/11/2016.
//  Copyright © 2016 ZED. All rights reserved.
//

#import "UIImage+ZUtilities.h"

@implementation UIImage (ZUtilities)

+ (instancetype)zOriginalImageWithName:(NSString *)name
{
    UIImage *image = [self imageNamed:name];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

+ (instancetype)zRenderImageWithName:(NSString *)name
{
    UIImage *image = [self imageNamed:name];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

+ (instancetype)zImageWithName:(NSString *)name renderColor:(UIColor *)color
{
    UIImage *image = [self imageNamed:name];
    return [image zImageWithRenderColor:color];
}

- (instancetype)zImageWithRenderColor:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    [self drawInRect:rect];
    
    [color set];
    UIRectFillUsingBlendMode(rect, kCGBlendModeSourceIn);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

- (UIImage *)zImageNamed:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    NSString *desc = [NSString stringWithFormat:@"找不到与文件名\"%@\"所对应的图片", imageName];
    NSAssert(image != nil, desc);
    
    return image;
}

+ (UIImage *)zImageWithColor:(UIColor *)color size:(CGSize)size andRadius:(CGFloat)radius
{
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
    [color set];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:radius];
    [path fill];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

@end
